package com.ruoyi.kenuo.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * @author POLO_TANG
 * @version 1.0.0
 * @ClassName JdbcUtils
 * @description: TODO 获取数据库信息工具类
 * @date 2023年05月25日
 */
@Component
public class JdbcUtils {

    @Autowired
    private  Environment environment;
/*    @Value("${ruoyi.dataUrl}")
    private  String urls;
    @Value("${ruoyi.username}")
    private  String username;
    @Value("${ruoyi.password}")
    private  String password;*/

    public  HashMap<String, String> getDBInfo() {
        String url = environment.getProperty("spring.datasource.druid.master.url");
        String username = environment.getProperty("spring.datasource.druid.master.username");
        String password = environment.getProperty("spring.datasource.druid.master.password");
        String[] split = url.split(":");
        String host = String.format("%s:%s:%s", split[0], split[1], split[2]);
        String[] portSplit = split[3].split("/");
        String port = portSplit[0];

        String[] databaseSplit = portSplit[1].split("\\?");
        String dbName = databaseSplit[0];
        HashMap<String, String> result = new HashMap<>();
        result.put("url",url);
        result.put("host",host);
        result.put("port",port);
        result.put("dbName",dbName);
        result.put("userName",username);
        result.put("passWord",password);

        return result;
    }


}
